package com.mybook.app.controller;

import com.github.pagehelper.PageInfo;
import com.mybook.app.core.BaseResponse;
import com.mybook.app.core.UserContext;
import com.mybook.app.core.WebPageInfo;
import com.mybook.app.dto.BookInfoAndUserInfoDto;
import com.mybook.app.model.BookInfo;
import com.mybook.app.request.BookDiscoverRequest;
import com.mybook.app.request.BookRequest;
import com.mybook.app.response.*;
import com.mybook.app.service.BookService;
import com.mybook.app.service.UserService;
import com.mybook.app.utils.LatLngUtils;
import com.mybook.app.utils.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zjutsw on 2017/10/30.
 */
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;

    /**
     * 获取新书推荐
     * 3本
     *
     * @return
     */
    @PostMapping("recommend/new")
    public BaseResponse recommendNew() {
        Logger.debug(this, "获取新书推荐");
        List<NewBook> newBookList = bookService.recommendNew();
        return new BaseResponse(newBookList);
    }

    /**
     * 获取新书图书详情
     *
     * @return
     */
    @PostMapping("detail/new")
    public BaseResponse detailNew(@RequestBody BookRequest bookRequest) {
        Logger.debug(this, "获取新书详情 :");
        NewBookDetail newBookList = bookService.detailNew(bookRequest.getbId());
        return new BaseResponse(newBookList);
    }


    /**
     * 获取热书推荐
     * 9本
     *
     * @return
     */
    @PostMapping("recommend/hot")
    public BaseResponse recommendHot() {
        Logger.debug(this, "获取热书推荐");
        List<HotBook> hotBookList = bookService.recommendHot();
        return new BaseResponse(hotBookList);
    }

    /**
     * 获取热书详情
     *
     * @return
     */
    @PostMapping("detail")
    public BaseResponse detail(@RequestBody BookRequest bookRequest) {
        Logger.debug(this, "获取热书详情");
        BookInfoAndUserInfoAndCollectInfo bookInfoAndUserInfo = bookService.detailBook(bookRequest.getbId());
        return new BaseResponse(bookInfoAndUserInfo);
    }


    /**
     * 获取热书详情
     *
     * @return
     */
    @PostMapping("discover")
    public BaseResponse bookDiscover(@RequestBody BookDiscoverRequest bookDiscoverRequest) {
        Logger.debug(this, "图书发现");
        PageInfo<BookInfoAndUserInfoDto> bookInfos = bookService.
                bookDiscover(bookDiscoverRequest);

        List<BookInfoDetailWithDistance> bookInfoDetailWithDistances = bookInfos.getList().stream().map(bookInfo -> {
            BookInfoDetailWithDistance bookInfoDetailWithDistance = new BookInfoDetailWithDistance();
            BeanUtils.copyProperties(bookInfo, bookInfoDetailWithDistance);
            // 设置距离
            bookInfoDetailWithDistance.setDistance(LatLngUtils.getDistance(bookInfo.getCuUserInfo().getCoor(), userService.getCoor()));
            return bookInfoDetailWithDistance;
        }).collect(Collectors.toList());

        //bookInfos 按照距离排序

        bookInfoDetailWithDistances.sort((BookInfoDetailWithDistance bookInfoDetailWithDistance, BookInfoDetailWithDistance bookInfoDetailWithDistance2)
                -> bookInfoDetailWithDistance.getDistance().compareTo(bookInfoDetailWithDistance2.getDistance()));

        //BaseResponse
        BaseResponse baseResponse = new BaseResponse(bookInfoDetailWithDistances);
        baseResponse.setPageInfo(new WebPageInfo());
        BeanUtils.copyProperties(bookInfos, baseResponse.getPageInfo());
        return baseResponse;
    }

}
